package com.jsyh.maidu.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.GoodsInfoActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.BestNewHot;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/6/19.
 */

public class GoodsListMoreAdapter extends RecyclerView.Adapter<GoodsListMoreAdapter.GoodsListHolder> {

    private List<BestNewHot> datas;
    private Context context;
    private String type;

    public GoodsListMoreAdapter(Context context, List<BestNewHot> datas, String type) {
        this.context = context;
        this.datas = datas;
        this.type = type;
    }

    @Override
    public GoodsListHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_goods_list, parent, false);
            GoodsListHolder holder = new GoodsListHolder(view);
            return holder;


    }

    @Override
    public void onBindViewHolder(GoodsListHolder holder, final int position) {
        if (type.equals("home_goods")){
            Picasso.with(context).load(ConfigValue.IMG_IP + datas.get(position).getGoods_thumb()).into(holder.goodsImg);
        }else {
            Picasso.with(context).load(ConfigValue.IMG_IP + datas.get(position).getGoods_img()).into(holder.goodsImg);
        }
        holder.goodsName.setText(datas.get(position).getGoods_name());
        holder.goodsShopPrice.setText(datas.get(position).getShop_price());
        holder.goodsMarketPrice.setText(datas.get(position).getMarket_price());
        holder.goodsMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("goodsId", datas.get(position).getGoods_id());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class GoodsListHolder extends RecyclerView.ViewHolder {

        ImageView goodsImg;
        TextView goodsName;
        TextView goodsShopPrice;
        TextView goodsMarketPrice;
        View view;

        public GoodsListHolder(View itemView) {
            super(itemView);
            view = itemView;
            goodsImg = (ImageView) itemView.findViewById(R.id.goods_list_img);
            goodsName = (TextView) itemView.findViewById(R.id.goods_list_goods_name);
            goodsShopPrice = (TextView) itemView.findViewById(R.id.goods_list_shop_price);
            goodsMarketPrice = (TextView) itemView.findViewById(R.id.goods_list_market_price);

        }
    }
    public void addAll(List<BestNewHot> elem) {
        datas.addAll(elem);
    }

}
